/**
 * 
 * @param {Object} config 配置对象
 * @param {Function} callback 回调函数
 */
function observerHandler(config = {}, callback) {
    const els = config.els;
    const threshold = config.threshold;
    const observer = new IntersectionObserver(fn, { threshold: threshold });

    function fn(ele) {
        ele.forEach(i => {
            if (i.intersectionRatio > 0 || i.isIntersecting) {
                callback(i.target)
                observer.unobserve(i.target)
            }
        })
    };
    els.forEach(i => observer.observe(i))
};
/**
 * 
 * @param {Object} config 配置对象
 * @param {Function} callback 回调函数
 */
export default function Observer(config = {}, callback) {
    if (!config.els.length) return console.warn('元素数组为空');
    config.threshold || (config.threshold = 0);
    config.els.forEach(i => {
        i.classList.add('animate__animated');
        i.style.visibility = 'hidden';
    });
    if (window.IntersectionObserver) {
        return observerHandler(config, callback)
    } else {
        let lazyLoadCopy = lazyLoad(config, callback);
        window.addEventListener('scroll', lazyLoadCopy)
        return lazyLoadCopy;
    }
}
export {
    observerHandler,
    Observer
}